package com.chat.biz.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chat.api.dto.AuthAddFriendRequestDTO;
import com.chat.api.dto.AuthAgreeFriendRequestDTO;
import com.chat.api.dto.AuthFriendRequestPageDTO;
import com.chat.api.entity.FriendRelation;
import com.chat.api.entity.FriendRequest;
import com.chat.api.vo.AuthFriendRequsetPageVO;
import com.chat.biz.mapper.FriendRequestMapper;
import com.chat.common.exception.CommonException;
import com.chat.common.result.ResultEnum;
import com.chat.common.threadLocal.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/*
*功能：
 作者：chenhao
*日期： 2025/6/7 下午7:53
*/
@Service
public class FriendRequestService extends ServiceImpl<FriendRequestMapper, FriendRequest> {

    @Autowired
    private FriendRelationService friendRelationService;
    public void addRequest(AuthAddFriendRequestDTO dto) {
        String FromUserId = UserContext.getUserId().getUserId();
        LambdaQueryWrapper<FriendRequest> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FriendRequest::getFromUserId,FromUserId);
        queryWrapper.eq(FriendRequest::getToUserId,dto.getToUserId());
        queryWrapper.eq(FriendRequest::getIsDeleted,false);
        FriendRequest one = baseMapper.selectOne(queryWrapper);
        if(one != null){
            throw new CommonException(ResultEnum.Friend_REQUEST_IS_SEND.getMsg());
        }
        FriendRequest friendRequest = new FriendRequest();
        friendRequest.setFromUserId(FromUserId);
        friendRequest.setToUserId(dto.getToUserId());
        friendRequest.setApply_reason(dto.getApply_reason());
        friendRequest.setStatus(0);
        friendRequest.setApplyTime(LocalDateTime.now());
        baseMapper.insert(friendRequest);
    }

    public Page<AuthFriendRequsetPageVO> FriendRequestPage(Page page, AuthFriendRequestPageDTO dto) {
        Page<AuthFriendRequsetPageVO> authFriendRequsetPageVO = baseMapper.FriendRequestPage(page, dto);
        return authFriendRequsetPageVO;
    }
    @Transactional
    public void handlerequest(AuthAgreeFriendRequestDTO dto) {
       FriendRequest friendRequest = baseMapper.selectById(dto.getFriendRequestId());
       if(friendRequest == null){
           throw new CommonException(ResultEnum.NULL_PARAM.getMsg());
       }
       if(!Objects.equals(friendRequest.getToUserId(), UserContext.getUserId().getUserId())){
           throw new CommonException(ResultEnum.AUTH_ERROR.getMsg());
       }
       if(friendRequest.getStatus()!=0){
           throw new CommonException(ResultEnum.Friend_REQUEST_HANDLED.getMsg());
       }
       friendRequest.setStatus(dto.getStatus());
       friendRequest.setHandleTime(LocalDateTime.now());
       baseMapper.updateById(friendRequest);
       if(dto.getStatus()==1){
           FriendRelation friendRelationTo = new FriendRelation();
           friendRelationTo.setUserId(friendRequest.getFromUserId());
           friendRelationTo.setFriendId(friendRequest.getToUserId());

           FriendRelation friendRelationFrom = new FriendRelation();
           friendRelationFrom.setUserId(friendRequest.getToUserId());
           friendRelationFrom.setFriendId(friendRequest.getFromUserId());
           friendRelationService.save(friendRelationTo);
           friendRelationService.save(friendRelationFrom);
       }


    }


    public List<FriendRequest> selectList(String toUserId) {
        return baseMapper.selectList(new LambdaQueryWrapper<FriendRequest>().eq(FriendRequest::getToUserId,toUserId));
    }
}
